package com.example.admin.controller.sys;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.admin.entity.SysRole;
import com.example.admin.service.ISysRoleService;
import com.example.util.APIState;
import com.example.util.AjaxResult;
import com.example.util.CurrentUser;
import com.example.util.Pager;

/**
 * <p>
 * 系统角色信息表 前端控制器
 * </p>
 *
 * @author wangkai
 * @since 2019-06-04
 */
@RestController
@RequestMapping("/sys/sysRole")
public class SysRoleController {

	@Resource(name = "sysRoleService")
	private ISysRoleService service;

	/**
	 * 获取角色
	 * 
	 * @param json
	 * @return
	 */
	@SuppressWarnings("rawtypes")
	@RequestMapping(value = "/page")
	public AjaxResult page(Pager pf) {
		Page<SysRole> page = new Page<SysRole>();
		page.setCurrent(pf.getPage());
		page.setSize(pf.getLimit());
		QueryWrapper<SysRole> queryWrapper = new QueryWrapper<SysRole>();
		pf = new Pager<SysRole>(service.page(page, queryWrapper));
		return new AjaxResult(APIState.S00, pf);
	}

	/**
	 * 保存
	 * 
	 * @param permission
	 * @return
	 */
	@RequestMapping(value = "/save")
	public AjaxResult save(SysRole sysRole) {
		if (sysRole != null) {
			sysRole.setCreateDate(new Date());
			sysRole.setCreateUserId(CurrentUser.getAdminUser().getId());
			if (service.saveRole(sysRole)) {
				return new AjaxResult(APIState.S00, "保存成功");
			}
		}
		return new AjaxResult(APIState.S01, "保存失败");
	}

	/**
	 * 修改
	 * 
	 * @param sysRole
	 * @return
	 */
	@RequestMapping(value = "/update")
	public AjaxResult update(SysRole sysRole) {
		if (sysRole != null) {
			if (service.updateRole(sysRole)) {
				return new AjaxResult(APIState.S00, "保存成功");
			}
		}
		return new AjaxResult(APIState.S01, "保存失败");
	}

	/**
	 * 删除
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "/{id}/delete")
	public AjaxResult delete(@PathVariable Long id) {
		if (id != null) {
			SysRole sysRole = service.getById(id);
			if (sysRole != null) {
				if (service.deleteRole(sysRole)) {
					return new AjaxResult(APIState.S00, "保存成功");
				}
			} else {
				return new AjaxResult(APIState.S01, "当前类型不存在请重新刷新列表");
			}
		}
		return new AjaxResult(APIState.S01, "保存失败");
	}

	/**
	 * 获取角色信息
	 * 
	 * @return
	 */
	@RequestMapping(value = "/list/role")
	public List<SysRole> getList() {
		QueryWrapper<SysRole> queryWrapper = new QueryWrapper<SysRole>();
		return service.list(queryWrapper);
	}

}
